import java.util.Iterator;
import java.util.List;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.OperationClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.MessageContext;
import org.dom4j.*;

public class Test {

    public static void main(String[] args) throws Exception {
        ServiceClient client = new ServiceClient();
        OperationClient operationClient = client.createClient(ServiceClient.ANON_OUT_IN_OP);
        //creating message context
        MessageContext outMsgCtx = new MessageContext();
        //assigning message context’s option object into instance variable
        Options opts = outMsgCtx.getOptions();
        //setting properties into option
        opts.setTo(new EndpointReference("http://localhost:8080/ListaEsamiWSService/ListaEsamiWS?wsdl"));
        opts.setAction("urn:getListaEsamiByMatricola");
        outMsgCtx.setEnvelope(creatSOAPEnvelope());
        operationClient.addMessageContext(outMsgCtx);

        System.out.println(outMsgCtx.getEnvelope());
        
        operationClient.execute(true);
        MessageContext inMsgtCtx = operationClient.getMessageContext("In");
        SOAPEnvelope response = inMsgtCtx.getEnvelope();

        String s = response.toString().replace("&lt;", "<");
        s = s.replace("&gt;", ">");
        Document d = DocumentHelper.parseText(s);

        List list = d.selectNodes("//esame");

        for (Iterator iter = list.iterator(); iter.hasNext(); ) {
            Element el = (Element) iter.next();
            System.out.println(el.selectSingleNode("nome").getText());
            System.out.println(el.selectSingleNode("descrizione").getText());
            System.out.println(el.selectSingleNode("crediti").getText());
            System.out.println(el.selectSingleNode("docente").getText());
        }
    }

    public static SOAPEnvelope creatSOAPEnvelope() {
        SOAPFactory fac = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope envelope = fac.getDefaultEnvelope();
        OMNamespace omNs = fac.createOMNamespace("http://webservices/", "xs");
        OMNamespace omNs2 = fac.createOMNamespace("", "");
        // creating the payload
        OMElement method = fac.createOMElement("getListaEsamiByMatricola", omNs);
        OMElement value1 = fac.createOMElement("matricola", omNs2);
        value1.setText("1");
        OMElement value2 = fac.createOMElement("user", omNs2);
        value2.setText("st");
        method.addChild(value1);
        method.addChild(value2);
        envelope.getBody().addChild(method);
        return envelope;
    }
}